<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/08/07 05:20:29 $)
// $RCSfile: client_message.php,v $ : $Revision: 1.11 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

require_once ("./includes/functions_departments.php");
$departments = new Departments;

$template->loadLanguageSection("livesupportclient");

if ($eventaction == "leaveamessage") {
	$template->cache(array('chathtmlheader', 'leavemessageconfirmation', 'departmentstatuscombo', 'messagecontentframe'));
	$template->assign("department", $departments->getDepartmentNameFromId($_POST["departmentid"]));
	$template->assign("fullname", htmlspecialchars($_POST["fullname"]));
	$template->assign("email", htmlspecialchars($_POST["email"]));
	$template->assign("subject", htmlspecialchars($_POST["subject"]));
	$template->assign("message", nl2br(htmlspecialchars($_POST["message"])));

	if (trim($_POST["subject"]) == "" || trim($_POST["fullname"]) == "" || trim($_POST["email"]) == "" || trim($_POST["message"]) == "")
	{
		$template->assign("error", $_SWIFT["language"]["requiredfieldempty"]);
		$eventaction = "messagecontentframe";
	} else if (!isValidEmail($_POST["email"])) {
		$template->assign("error", $_SWIFT["language"]["lsinvalidemail"]);
		$eventaction = "messagecontentframe";
	} else {
		require_once ("./modules/livesupport/functions_livesupport.php");
		require_once ("./modules/livesupport/functions_messages.php");

		if ($_SWIFT["settings"]["livesupport_fwmessageemail"] != "" && $_SWIFT["settings"]["livesupport_deletemessageafterfw"] == 1)
		{
			// Dont add a message as we have set it to delete the msg and forward it to some email address	
		} else {
			// All ok, insert the message
			clientLeaveMessage($_POST["subject"], $_POST["fullname"], $_POST["email"], $_POST["departmentid"], $_POST["message"]);			
		}

		echo $template->displayTemplate("chathtmlheader");
		echo $template->displayTemplate("leavemessageconfirmation");

		/**
		* ###############################################
		* BEGIN BETA TESTING CODE
		* ###############################################
		*/
		if ($_SWIFT["settings"]["livesupport_fwmessageemail"] != "")
		{
			
			require_once ("./includes/functions_mail.php");
			$mail = new SWIFT_Mail();

			$emailtext = $_POST["message"];
			$emailhtml = nl2br($_POST["message"]);
		
			$mail->setSubject($_POST["subject"]);
			$mail->setData($emailhtml, $emailtext);

			$mail->sendMail($_SWIFT["settings"]["livesupport_fwmessageemail"], $_POST["email"], $_POST["fullname"]);
		}
		/**
		* ###############################################
		* END BETA TESTING CODE
		* ###############################################
		*/
	}
}

if ($eventaction == "messagecontentframe")
{
	$template->cache(array('chathtmlheader', 'messagecontentframe', 'departmentstatuscombo'));
	require_once ("./modules/livesupport/functions_livesupport.php");
	
	if (empty($_GET["departmentid"]))
	{
		unset($_GET["departmentid"]);
	}

	$template->assign("departmentid", $_GET["departmentid"]);

	$depresult = getDepartmentsStatus($_GET["departmentid"]);
	$template->assign("onlinedepartments", $depresult["online"]);
	$template->assign("offlinedepartments", $depresult["offline"]);
	$template->assign("fullname", $_GET["fullname"]);
	$template->assign("email", $_GET["email"]);

	echo $template->displayTemplate("chathtmlheader");
	echo $template->displayTemplate("messagecontentframe");
}
?>